package com.rjj.wlqq.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @author renjianjun
 * @date 2020/8/13 19:40
 * @email 215060580@qq.com
 * 日志切面
 *
 * @Aspect
 * @Component
 * 两个注解都需要添加否则不生效
 */
@Aspect
@Component
public class LogAspectAnnotation {

    /**
     * 切点：定位点，那里添加了注解，那里就是我们切入的点位
     * 就是那个注解
     * 
     * @annotation(com.rjj.wlqq.aop.aspect.Log) == @annotation(Log) 回自己找到的
     */
    @Pointcut(value = "@annotation(com.rjj.wlqq.aop.aspect.Log)")
    public void service(){
    }

    /**
     * * 增强（Advice）         - 织入到目标类连接点上的一段程序代码。除了一段程序代码外，还拥有执行点的方位信息。
     *  切入注解，在这个注解所在的方法执行之前执行
     */
    @Before("service()")
    public void before(JoinPoint joinPoint){
        // 接收到请求，记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 记录下请求内容
        System.out.println("URL : " + request.getRequestURL().toString());
        System.out.println("HTTP_METHOD : " + request.getMethod());
        System.out.println("IP : " + request.getRemoteAddr());
        System.out.println("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        System.out.println("ARGS : " + Arrays.toString(joinPoint.getArgs()));        
        System.out.println("before service ...");
    }

    /**
     * 围绕切入点
     * 
     */
    @Around("@annotation(log)")
    public Object around(ProceedingJoinPoint joinPoint,Log log){
        System.out.println("around 注解的值" + log.value());
        try {
            System.out.println("around 之前");
            Object proceed = joinPoint.proceed();
            System.out.println("around 之后");
            return proceed + " hello word";
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }



    @AfterReturning(returning = "ret", pointcut = "service()")
    public void doAfterReturning(Object ret) {
        // 处理完请求，返回内容
        System.out.println("方法的返回值 : " + ret);
    }

    /**
     *     后置异常通知
     */
    @AfterThrowing("service()")
    public void throwss(JoinPoint jp){
        System.out.println("方法异常时执行.....");
    }

    /**
     后置最终通知,final增强，不管是抛出异常或者正常退出都会执行
     */
    @After("service()")
    public void after(JoinPoint jp){
        System.out.println("方法最后执行.....");
    }

    /**
     * 环绕通知,环绕增强，相当于MethodInterceptor
     */
    @Around("service()")
    public Object arround(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("方法环绕start.....");
        try {
            Object o =  pjp.proceed();
            System.out.println("方法环绕proceed，结果是 :" + o);
            return o;
        } catch (Throwable e) {
            throw e;
        }
    }
}
